import json

import requests as rq

at = 'pat_3ZWkeMq1epizlol1tVkMISCQMhCTUncRWBT270iM4iySq2YH7DKprCSwWQr8C2tT'

url = 'https://api.coze.com/open_api/v2/chat'
botid = '7346504075225628674'

headers = {
    'Authorization': 'Bearer ' + at,
    'Content-Type': 'application/json',
    'Accept': '*/*',
    'Host': 'api.coze.com',
    'Connection': 'keep-alive',
}

data = {
    'conversation_id': '1',
    'bot_id': botid,
    'user': '29032201862555',
    'stream': False
}


def get_data(question):
    data['query'] = question
    return data



def get_prompt_result(prompt):
    res = rq.post(url, headers=headers, json=get_data(prompt)).json()
    messages = res['messages']
    answers = [m['content'] for m in messages if m['type'] == 'answer']
    final_answer = answers[0] if len(answers) > 0 else None
    try:
        final_data = json.loads(final_answer)
        print([news for news in final_data])
        print('-' * 50)
    except:
        print('Failed to parse json response')
    pass


prompt = 'Please provide a json response for following query: can you search for ideas to make money as a software developer company of 1 person,  (at least 5 of them) and summarize them in single line, and then put them in a json list. remember to only return json result, not any explaination and any other words, for this is being used for json parsing, and other words will break it, each news should be a string and I need a list strings as a result, not keys, not any other fields. please return the results in Chinese'
for i in range(10):
    print(f'testing for {i}th time')
    get_prompt_result(prompt)
